package com.bkgy.jobs;

import com.bkgy.controller.lzDaNew.mapper.ILzDaNewMapper;
import com.bkgy.controller.lzDaNew.entity.LzDaNew;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

import static java.lang.Math.pow;

/**
 * Author:Lipeng Zhao
 * Description:
 *
 * @Date:Created In 16:16 2025/8/12
 */
@Slf4j
@Service
public class RoughnessCalculateJobs {

    @Autowired
    ILzDaNewMapper lzDaNewDao;

    public void run() {
        double R0_mid=3;
        double R0_os=3;
        double R0_ds=3;
        double R;    //最近录入的粗糙度
        double R_os;    //最近录入的粗糙度
        double R_ds;    //最近录入的粗糙度
        double B;  //粗糙度系数
        double B_os;  //粗糙度系数
        double B_ds;  //粗糙度系数
        double B0=0.0000225;  //原始粗糙度系数
        double B0_os=0.0000225;  //原始粗糙度系数
        double B0_ds=0.0000225;  //原始粗糙度系数
        double L; //轧制长度
        double roughness_mid;
        double roughness_ds;
        double roughness_os;
        List<LzDaNew> list= lzDaNewDao.getLzDaNewByStatus();
        for(int i=0;i<list.size();i++){
            L=Double.parseDouble(list.get(i).getRollingKilometers());
            if(!StringUtils.isEmpty(list.get(i).getRough())){
                R=Double.parseDouble(list.get(i).getRough());
            }else {
                R=R0_mid * pow((L*B0 + 1), -1);
            }

            if(!StringUtils.isEmpty(list.get(i).getRoughOs())){
                R_os=Double.parseDouble(list.get(i).getRoughOs());
            }else {
                R_os=R0_os * pow((L*B0_os + 1), -1);
            }

            if(!StringUtils.isEmpty(list.get(i).getRoughDs())){
                R_ds=Double.parseDouble(list.get(i).getRoughDs());
            }else {
                R_ds=R0_ds * pow((L*B0_ds + 1), -1);
            }


            if(!StringUtils.isEmpty(list.get(i).getBMid())){
                if(!StringUtils.isEmpty(list.get(i).getOriginalRoughCoefficient())&&!list.get(i).getOriginalRoughCoefficient().equals(list.get(i).getBMid())){
                    B=Double.parseDouble(list.get(i).getOriginalRoughCoefficient());
                }else {
                    B=Double.parseDouble(list.get(i).getBMid());
                }
            }else {
                B = (R0_mid / R - 1) / L;
                B = (B + B0) / 2;
            }

            if(!StringUtils.isEmpty(list.get(i).getBDs())){
                if(!StringUtils.isEmpty(list.get(i).getOriginalRoughCoefficient())&&!list.get(i).getOriginalRoughCoefficient().equals(list.get(i).getBDs())){
                    B_ds=Double.parseDouble(list.get(i).getOriginalRoughCoefficient());
                }else {
                    B_ds=Double.parseDouble(list.get(i).getBDs());
                }
            }else {
                B_ds = (R0_ds / R_ds - 1) / L;
                B_ds = (B_ds + B0_ds) / 2;
            }
            if(!StringUtils.isEmpty(list.get(i).getBOs())){
                if(!StringUtils.isEmpty(list.get(i).getOriginalRoughCoefficient())&&!list.get(i).getOriginalRoughCoefficient().equals(list.get(i).getBOs())){
                    B_os=Double.parseDouble(list.get(i).getOriginalRoughCoefficient());
                }else {
                    B_os=Double.parseDouble(list.get(i).getBOs());
                }
            }else {
                B_os = (R0_os / R_os - 1) / L;
                B_os = (B_os + B0_os) / 2;
            }
            list.get(i).setBMid(String.valueOf(B));
            list.get(i).setBDs(String.valueOf(B_ds));
            list.get(i).setBOs(String.valueOf(B_os));
            //利用粗糙度衰减公式  计算粗糙度
            roughness_mid = R0_mid * pow((L*B + 1), -1);
            roughness_ds=R0_ds * pow((L*B_ds + 1), -1);
            roughness_os=R0_os * pow((L*B_os + 1), -1);
            list.get(i).setRoughMidNow(String.valueOf(roughness_mid));
            list.get(i).setRoughDsNow(String.valueOf(roughness_ds));
            list.get(i).setRoughOsNow(String.valueOf(roughness_os));
            lzDaNewDao.updateEntity(list.get(i));
        }


    }
}
